--[[
FilePath: \Lua\View\UIChapterRewardNew\UIChapterRewardNew.lua
新版章节奖励
--]]

require 'View/UIBase/UIBase'

UIChapterRewardNew = UIBase.New(PanelResNames.UIChapterRewardNew)
local _M = UIChapterRewardNew

_M.hasInit = false
_M.ContentItems = {}
_M.pageIndex = 0
_M.curChapter = 1;
_M.canClick = false;
_M.rewardType = 0;

function _M.Init()
    UIBase.Init(_M)
end

function _M:OnCreate()
    if not _M.hasInit then
        self:RegisterOnClick(self:GetChild("bgClose"), self.OnBack)
        self.title = self:GetChild("title");
        self.decs = self:GetChild("Decs");
        self.rewardList = self:GetChild("rewardList");
        self.btnRight = self:GetChild("btnRight");
        self.btnLeft = self:GetChild("btnLeft");
        self.model = self:GetChild("model");
        self.barState = self:GetChild("bar5"):GetController("state");
        self.btnType = self:GetController("c1");

        self.bar = {};
        for i = 1, 4 do
            table.insert(self.bar, self:GetChild("bar"..i));
        end

        _M.hasInit = true

        self:addEvent()
    end
    self:initView();
end

function _M:addEvent()
    self.btnRight.onClick:Add(self.onClickRight,self);
    self.btnLeft.onClick:Add(self.onClickLeft,self);
end

function _M:removeEvent()
    self.btnRight.onClick:Remove(self.onClickRight);
    self.btnLeft.onClick:Remove(self.onClickLeft);
end

function _M:onClickRight()
    if not self.canClick then
        return;
    end
    self.canClick = false;
    self.pageIndex = self.pageIndex + 1;
    self:refreshReward();
end

function _M:onClickLeft()
    if not self.canClick then
        return;
    end
    self.canClick = false;
    self.pageIndex = self.pageIndex - 1;
    if self.pageIndex < 0 then
        self.pageIndex = 0;
    end
    self:refreshReward();
end

function _M:initView()
    self.canClick = false;
    if self.ContentItems == nil then
        self.ContentItems = {};
    end
    self.rewardList:RemoveChildrenToPool()
    for i = 1, 4 do
        local itemObj = self.rewardList:AddItemFromPool();
        local item = self.ChapterRewardItem.New();
        self.ContentItems[i] = item;
        item:OnCreate(itemObj,self);
    end
    local isEndLevel = logicMgr.MapManager.IsEndChapterMapId(MapManager.getMapId());
    self.curChapter = logicMgr.MapManager.GetCurrentChapterId();
    if isEndLevel then
        local hasReward, rewardGet = logicMgr.MapManager.GetChapterReward(MapManager.getMapId());
        local nextChapterTaskCfg = logicMgr.MapManager.GetChapterRewardCfg(self.curChapter + 1);
        if (hasReward and not rewardGet) or (nextChapterTaskCfg == nil) then
            --当前章节奖励未领取
            self.curChapter = self.curChapter;
        else
            --显示下一章节奖励
            self.curChapter = self.curChapter + 1;
        end
    end
    local dayIndex = self.curChapter % 4;
    self.pageIndex = math.modf(self.curChapter / 4);
    if dayIndex == 0 then
        self.pageIndex = self.pageIndex - 1;
    end
    if self.pageIndex < 0 then
        self.pageIndex = 0;
    end
    self.title.text = getLanguage("ChapterRewardText5");
    self.heroModelId = dataTable.getGlobalInt(525);
    self.heroScale = dataTable.getGlobalString(526);
    self.genes = decodeJsonStrToTabel(dataTable.getGlobalString(527));
    self.genesScale = dataTable.getGlobalString(528);
    self:refreshReward();
end

function _M:refreshReward()
    local isMaxChapter = false;
    local minChapter = (self.pageIndex * 4) + 1;
    local maxChapter = (self.pageIndex * 4) + 4;
    local localRewardType = 0;
    self.barState.selectedIndex = 0;
    if minChapter > self.curChapter then
        for i = 1, 4 do
            local chapter = (self.pageIndex * 4) + i;
            local maxMapCfg = MapManager.GetChapterMaxStageNum(chapter);
            if maxMapCfg == nil or maxMapCfg.f_NextStage == nil or tonumber(maxMapCfg.f_NextStage) < 1  then
                isMaxChapter = true;
                self.bar[i].value = 0;
            end
            local taskCfg = logicMgr.MapManager.GetChapterRewardCfg(chapter);
            local reward = nil;
            if taskCfg ~= nil and taskCfg.f_Award ~= nil and taskCfg.f_Award ~= "" then
                reward = decodeJsonStrToTabel(taskCfg.f_Award)[1];
                localRewardType = taskCfg.f_ChapterRewardType;
            end
            self.ContentItems[i]:OnShow(maxMapCfg, reward, 1);
            self.bar[i].value = 0;
        end
    elseif maxChapter < self.curChapter then
        for i = 1, 4 do
            local chapter = (self.pageIndex * 4) + i;
            local maxMapCfg = MapManager.GetChapterMaxStageNum(chapter);
            local taskCfg = logicMgr.MapManager.GetChapterRewardCfg(chapter);
            local reward = nil;
            if taskCfg ~= nil and taskCfg.f_Award ~= nil and taskCfg.f_Award ~= "" then
                reward = decodeJsonStrToTabel(taskCfg.f_Award)[1];
                localRewardType = taskCfg.f_ChapterRewardType;
            end
            self.ContentItems[i]:OnShow(maxMapCfg, reward, 2);
            self.bar[i].value = 100;
        end
        self.barState.selectedIndex = 1;
    else
        for i = 1, 4 do
            local chapter = (self.pageIndex * 4) + i;
            local maxMapCfg = MapManager.GetChapterMaxStageNum(chapter);
            local taskCfg = logicMgr.MapManager.GetChapterRewardCfg(chapter);
            local reward = nil;
            local serverData = nil;
            local state = 2;
            if taskCfg ~= nil and taskCfg.f_Award ~= nil and taskCfg.f_Award ~= "" then
                reward = decodeJsonStrToTabel(taskCfg.f_Award)[1];
                serverData = logicMgr.TaskManager.getTaskServerDataById(taskCfg.f_TaskId);
                localRewardType = taskCfg.f_ChapterRewardType;
            end
            if maxMapCfg == nil or maxMapCfg.f_NextStage == nil or tonumber(maxMapCfg.f_NextStage) < 1 then
                isMaxChapter = true;
                self.bar[i].value = 0;
            else
                if chapter < self.curChapter then
                    state = 2;
                    self.bar[i].value = 100;
                elseif chapter == self.curChapter then
                    local isEndLevel = logicMgr.MapManager.IsEndChapterMapId(MapManager.getMapId());
                    if isEndLevel then
                        if serverData ~= nil and serverData.status == -1 then --已领取
                            state = 2;
                        else
                            state = 1;
                        end
                    else
                        state = 1;
                    end
                    local curNum;
                    if logicMgr.MapManager.GetCurrentChapterId() ~= self.curChapter then
                        curNum = 0;
                    else
                        local mapCfg = logicMgr.MapManager.getMapcfg();
                        if mapCfg == nil then
                            curNum = 0;
                        else
                            curNum = mapCfg.f_StageNum;
                        end
                    end
                    local maxNum = maxMapCfg.f_StageNum;
                    self.bar[i].value = math.min((curNum / maxNum * 100), 100);
                else
                    state = 1;
                    self.bar[i].value = 0;
                end
            end
            self.ContentItems[i]:OnShow(maxMapCfg, reward, state);
        end
    end
    if isMaxChapter then
        self.btnType.selectedIndex = 2;
    elseif self.pageIndex == 0 then
        self.btnType.selectedIndex = 0;
    else
        self.btnType.selectedIndex = 1;
    end

    self.decs.text = getLanguage("ChapterRewardType"..localRewardType);
    if localRewardType == 2 then
        if self.rewardType == 0 or self.rewardType ~= 2 then
            self:destroyPlayRole();
            self:showPetModel();
        end
    else
        if self.rewardType == 0 or self.rewardType == 2 then
            self:removePetModel();
            self:loadHero();
        end
    end
    self.rewardType = localRewardType;
    self.canClick = true;
end

function _M:loadHero()
    if self.heroModelId ~= nil and self.heroModelId > 0 then
        local imageName = logicMgr.HeroManager.GetHeroDrawingPath(self.heroModelId);
        if self.drawWrapper == nil then
            self.drawWrapper = GoWrapper.New();
        end
        resMgr:LoadModelPrefab(imageName, function(go)
            if(self.RoleLihui~=nil)then
                destroyImmediate(self.RoleLihui);
                self.RoleLihui = nil;
            end
            self.RoleLihui = newObject(go);
            self.RoleLihui.name = imageName;
            Util.AddAction(self.RoleLihui,"idle1",true,0.5);
            self.RoleLihui:AddComponent(TCanvas);
            self.drawWrapper:SetWrapTarget(self.RoleLihui,false);
            Util.Lua_SetLocalPositionXYZ(self.RoleLihui.transform,self.heroScale[3],self.heroScale[4],0);
            Util.Lua_SetLocalScaleXYZ(self.RoleLihui.transform,self.heroScale[1],self.heroScale[2],self.heroScale[1]);
            self.model:SetNativeObject(self.drawWrapper);
        end);
    end
end

function _M:showPetModel()
    if #self.genes == PetManager.PetPart.Ornament then
        local petModelInfo = {};
        petModelInfo.parentGO = self.model;
        resMgr:LoadModelPrefab(PetManager.PetModelPrefab, function(go)
            self:removePetModel()

            self.PetModelGO = newObject(go);

            local com = self.PetModelGO:GetComponent("SkeletonGraphic")

            if self.PetModelGO and com then
                com.material = resMgr.Pet_Copy_Mat
                com.material:SetFloat("_StencilComp",8)
                com.material:SetFloat("_Stencil",1);
            end

            petModelInfo.modelGO = self.PetModelGO;
            if self.goWrapper == nil then
                self.goWrapper = GoWrapper.New();
            end
            petModelInfo.modelWrapper = self.goWrapper;
            PetManager.PetModelFirstSet(petModelInfo, self.genes, true, Vector3.New(self.genesScale[3], self.genesScale[4], 0), Vector3.New(self.genesScale[1], self.genesScale[2], 1));
        end)
    end
end

-- 显示
function _M:OnEnable()

end

-- 隐藏
function _M:OnDisable()

end

function _M:destroyPlayRole()
    if self.RoleLihui ~= nil then
        destroy(self.RoleLihui);
        self.RoleLihui = nil;
    end
end

function _M:removePetModel()
    if self.PetModelGO then
        destroy(self.PetModelGO);
        self.PetModelGO = nil;
    end
end

-- 卸载
function _M:OnDestroy()
    self:removeEvent();
    if self.ContentItems then
        for key, value in pairs(self.ContentItems) do
            value:OnDestroy();
        end
        self.ContentItems = nil;
    end
    self:destroyPlayRole();
    if self.drawWrapper then
        self.drawWrapper:Dispose();
        self.drawWrapper = nil;
    end
    self:removePetModel();
    if self.goWrapper then
        self.goWrapper:Dispose();
        self.goWrapper = nil;
    end
    self.canClick = false;
    self.hasInit = false;
    self.pageIndex = 0;
    self.rewardType = 0;
end


-- 奖励item
_M.ChapterRewardItem = Class("ChapterRewardItem",UIComponentInterface)
local _M_T = _M.ChapterRewardItem
function _M_T:OnCreate(obj,parent)
    self.parent = parent
    self.fguiObj = obj

    self.reward = obj:GetChild("reward");
    self.tips = obj:GetChild("tips");
    self.stateCtrl = obj:GetController("state");
end
function _M_T:OnShow(maxMapCfg, reward, state)
    if reward ~= nil then
        logicMgr.CostManager.RefreshItem(self.reward, reward);
    end
    if maxMapCfg ~= nil then
        self.tips.text = string.format(getLanguage("ChapterRewardText4"), maxMapCfg.f_ChapterId.."-"..maxMapCfg.f_StageNum);
        self.stateCtrl.selectedIndex = state;
    else
        self.stateCtrl.selectedIndex = 0;
    end
end

function _M_T:OnDestroy()

end

return _M;